# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import pandas as pd
import coordinate
from utils.HttpUtils import HttpUtils

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
高得周边信息获取。通过高得API
账号：13841212966
密码：sunshine12966
https://lbs.amap.com/dev/key/app#
API文档
https://lbs.amap.com/api/webservice/guide/api/search
AOI
https://www.amap.com/detail/get/detail?id=B0014014AD&smToken=token&smSign=undefined
"""
class ZhouBian():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Cookie": "JSESSIONID=5DD8E23F63545D703304D7D8B22E40D3",
            "DNT": "1",
            "Host": "bdww.sach.gov.cn",
            "Origin": "http://bdww.sach.gov.cn",
            "Referer": "http://bdww.sach.gov.cn/w/list",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
            "X-Requested-With": "XMLHttpRequest"
        }
        # API Key  c0cf8a8c654e7cdb6b42f9a8c41f980d
        self.key = "4b86820a7590de60e4f81f53e59ae17f"

    # 关键字搜索
    def get_search(self, pagenum):
        postData = {
            "key": self.key,
            "keywords": "公交",#"小区",
            "types":"",  # 查询POI类型
            "city":"210302",#"杭州", 北京/beijing/010/110000
            # "citylimit":"", # 仅返回指定城市数据 true/false
            "extensions":"all",
            "offset": "25",
            "citylimit": "true",  # 仅返回指定城市数据
            "page":pagenum  # 最大翻页数100
        }

        url = "https://restapi.amap.com/v3/place/text"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        for d in data_json['pois']:
            dict_data = dict()
            dict_data['id'] = d['id']  # ID
            dict_data['name'] = d['name']  # name
            dict_data['type'] = d['type']  # 兴趣点类型 顺序为大类、中类、小类 例如：餐饮服务;中餐厅;特色/地方风味餐厅
            dict_data['typecode'] = d['typecode']  # 兴趣点类型编码
            dict_data['biz_type'] = d['biz_type']  # 行业类型
            dict_data['address'] = d['address']  # 地址
            dict_data['location'] = d['location']  # 经纬度
            dict_data['distance'] = d['distance']  # 离中心点距离
            dict_data['tel'] = d['tel']  # POI的电话
            dict_data['citycode'] = d['citycode']  # 城市编码
            dict_data['cityname'] = d['cityname']  # 城市名
            dict_data['adcode'] = d.get('adcode')  # 区域编码
            dict_data['adname'] = d.get('adname')  # 区域名称
            dict_data['alias'] = d.get('alias')  # 别名
            dict_data['tag'] = d.get('tag')  # 该POI的特色内容
            dict_data['business_area'] = d.get('business_area')  # 所在商圈
            dict_data['rating'] = d.get('biz_ext').get('rating')  # 评分  仅存在于餐饮、酒店、景点、影院类POI之下
            dict_data['cost'] = d.get('biz_ext').get('cost')  # 人均消费  仅存在于餐饮、酒店、景点、影院类POI之下
            num = 0
            for i in d.get('photos'):
                num += 1
                dict_data['photos_titile_' + str(num)] = i.get('titile')  # 图片介绍
                dict_data['photos_url_' + str(num)] = i.get('url')  # 具体链接
            print(str(dict_data))


    # 周边搜索，通过经纬度设定范围
    def get_around(self, pagenum):
        postData = {
            "key": self.key,
            "location": "116.473168,39.993015",  # 中心点坐标
            "keywords": "美食",
            "types":"",  # 查询POI类型
            "city":"北京",
            # "citylimit":"",
            "extensions":"all",
            "offset": "25",
            "page":pagenum  # 最大翻页数100
        }

        url = "https://restapi.amap.com/v3/place/around"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        for d in data_json['pois']:
            dict_data = dict()
            dict_data['id'] = d['id']  # ID
            dict_data['name'] = d['name']  # name
            dict_data['type'] = d['type']  # 兴趣点类型 顺序为大类、中类、小类 例如：餐饮服务;中餐厅;特色/地方风味餐厅
            dict_data['typecode'] = d['typecode']  # 兴趣点类型编码
            dict_data['biz_type'] = d['biz_type']  # 行业类型
            dict_data['address'] = d['address']  # 地址
            dict_data['location'] = d['location']  # 经纬度
            dict_data['distance'] = d['distance']  # 离中心点距离
            dict_data['tel'] = d['tel']  # POI的电话
            dict_data['citycode'] = d['citycode']  # 城市编码
            dict_data['cityname'] = d['cityname']  # 城市名
            dict_data['adcode'] = d.get('adcode')  # 区域编码
            dict_data['adname'] = d.get('adname')  # 区域名称
            dict_data['tag'] = d.get('tag')  # 该POI的特色内容
            dict_data['business_area'] = d.get('business_area')  # 所在商圈
            dict_data['rating'] = d.get('biz_ext').get('rating')  # 评分  仅存在于餐饮、酒店、景点、影院类POI之下
            dict_data['cost'] = d.get('biz_ext').get('cost')  # 人均消费  仅存在于餐饮、酒店、景点、影院类POI之下
            num = 0
            for i in d.get('photos'):
                num += 1
                dict_data['photos_titile_' + str(num)] = i.get('titile')  # 图片介绍
                dict_data['photos_url_' + str(num)] = i.get('url')  # 具体链接


    # 多边形搜索 为矩形时，可传入左上右下两顶点坐标对
    def get_polygon(self, pagenum):
        postData = {
            "key": self.key,
            "polygon": "116.460988,40.006919|116.48231,40.007381;116.47516,39.99713|116.472596,39.985227|116.45669,39.984989|116.460988,40.006919",
            "keywords": "美食",
            "types":"",  # 查询POI类型
            "city":"北京",
            # "citylimit":"",
            "extensions":"all",
            "offset": "25",
            "citylimit": "true",  # 仅返回指定城市数据
            "page":pagenum  # 最大翻页数100
        }

        url = "https://restapi.amap.com/v3/place/polygon"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        for d in data_json['pois']:
            dict_data = dict()
            dict_data['id'] = d['id']  # ID
            dict_data['name'] = d['name']  # name
            dict_data['type'] = d['type']  # 兴趣点类型 顺序为大类、中类、小类 例如：餐饮服务;中餐厅;特色/地方风味餐厅
            dict_data['typecode'] = d['typecode']  # 兴趣点类型编码
            dict_data['biz_type'] = d['biz_type']  # 行业类型
            dict_data['address'] = d['address']  # 地址
            dict_data['location'] = d['location']  # 经纬度
            dict_data['distance'] = d['distance']  # 离中心点距离
            dict_data['tel'] = d['tel']  # POI的电话
            dict_data['citycode'] = d['citycode']  # 城市编码
            dict_data['cityname'] = d['cityname']  # 城市名
            dict_data['adcode'] = d.get('adcode')  # 区域编码
            dict_data['adname'] = d.get('adname')  # 区域名称
            dict_data['alias'] = d.get('alias')  # 别名
            dict_data['tag'] = d.get('tag')  # 该POI的特色内容
            dict_data['business_area'] = d.get('business_area')  # 所在商圈
            dict_data['rating'] = d.get('biz_ext').get('rating')  # 评分  仅存在于餐饮、酒店、景点、影院类POI之下
            dict_data['cost'] = d.get('biz_ext').get('cost')  # 人均消费  仅存在于餐饮、酒店、景点、影院类POI之下
            num = 0
            for i in d.get('photos'):
                num += 1
                dict_data['photos_titile_' + str(num)] = i.get('titile')  # 图片介绍
                dict_data['photos_url_' + str(num)] = i.get('url')  # 具体链接


    # ID查询
    def get_search_id(self, id):
        postData = {
            "key": self.key,
            "id": id,  # 兴趣点ID
        }

        url = "https://restapi.amap.com/v3/place/detail"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        for d in data_json['pois']:
            dict_data = dict()
            dict_data['id'] = d['id']  # ID
            dict_data['name'] = d['name']  # name
            dict_data['type'] = d['type']  # 兴趣点类型 顺序为大类、中类、小类 例如：餐饮服务;中餐厅;特色/地方风味餐厅
            dict_data['typecode'] = d['typecode']  # 兴趣点类型编码
            dict_data['biz_type'] = d['biz_type']  # 行业类型
            dict_data['address'] = d['address']  # 地址
            dict_data['location'] = d['location']  # 经纬度
            dict_data['distance'] = d['distance']  # 离中心点距离
            dict_data['tel'] = d['tel']  # POI的电话
            dict_data['citycode'] = d['citycode']  # 城市编码
            dict_data['cityname'] = d['cityname']  # 城市名
            dict_data['adcode'] = d.get('adcode')  # 区域编码
            dict_data['adname'] = d.get('adname')  # 区域名称
            dict_data['tag'] = d.get('tag')  # 该POI的特色内容
            dict_data['business_area'] = d.get('business_area')  # 所在商圈
            if len(d.get('biz_ext')) > 0:
                dict_data['rating'] = d.get('biz_ext').get('rating')  # 评分  仅存在于餐饮、酒店、景点、影院类POI之下
                dict_data['cost'] = d.get('biz_ext').get('cost')  # 人均消费  仅存在于餐饮、酒店、景点、影院类POI之下
            num = 0
            for i in d.get('photos'):
                num += 1
                dict_data['photos_titile_' + str(num)] = i.get('titile')  # 图片介绍
                dict_data['photos_url_' + str(num)] = i.get('url')  # 具体链接
        x = []
        tmp = d['location'].split(",")
        lon = tmp[0]
        lat = tmp[1]
        lon1, lat1 = coordinate.gcj02_to_wgs84(float(lon), float(lat))
        # x.append([d['id'], d['name'], d['type'], d['address'], float(lon), float(lat)])
        x.append([d['id'], d['name'], d['type'], d['address'], lon1, lat1])
        c1 = pd.DataFrame(x)
        c1.to_csv('poi.csv', encoding='utf-8-sig')


    # 根据POI查询AOI
    def get_aoi(self, id):
        heads = {
            "authority": "www.amap.com",
            "method": "GET",
            "path": "/detail/get/detail?id=B0014014AD&smToken=token&smSign=undefined",
            "scheme": "https",
            "accept": "*/*",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9",
            # "amapuuid": "ac5d7f18-0d25-40d0-99b4-9ed0e944fd69",
            "cookie": "key=bfe31f4e0fb231d29e1d3ce951e2c780; guid=55f9-1be1-0817-1a05; UM_distinctid=16f1bd8c203464-02be557d023375-6701b35-144000-16f1bd8c20452a; CNZZDATA1255626299=2083320017-1576716524-https%253A%252F%252Fwww.baidu.com%252F%7C1576716524; cna=O8WBFiL8U04CAXd0SKl8cV1h; _uab_collina=157672018937775910649034; x5sec=7b22617365727665723b32223a223666383432376537346439376662336536643138386433383965633565643161434c4858362b3846454b766978376e332f616a7a3641453d227d; l=dBQgvLpeqqpNccfhBOfZhurza77OuQRf1dVzaNbMiICP9E1w5-ehWZLZh-TeCnhVH65wR3PBVv73BJ8dMyC4Z1u2AC1hE_at3dC..; isg=BAwM02J3vaWJBanZwYBpduhI3Wo-RbDvgkE5L2bPiLda8a_7jlUhf2QHkLnskOhH",
            "dnt": "1",
            "referer": "https://www.amap.com/place/B0014014AD",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
            "x-csrf-token": "null",
            "x-requested-with": "XMLHttpRequest"
        }
        postData = {
            "id": id,  # POI ID
            "smToken": "token",
            "smSign": "undefined",
        }

        url = "https://www.amap.com/detail/get/detail"
        html = HttpUtils.do_request("GET", url, heads, postData)
        data_json = json.loads(html.text)
        shape = data_json['data']['spec']['mining_shape']['shape']
        shape_list = shape.split(";")
        x = []
        for s in shape_list:
            tmp = s.split(",")
            lon = tmp[0]
            lat = tmp[1]
            x.append(["B0014014AD", "白云山风景名胜区", "风景名胜;风景名胜;国家级景点", "广园中路801号", lon, lat])
        c1 = pd.DataFrame(x)
        c1.to_csv('aoi.csv', encoding='utf-8-sig')

    # 地址转geo地理编码
    def get_geo(self, address):
        postData = {
            "key": self.key,
            "address": address,  # 地址
            "output": "json",  # 返回json
        }
        url = "https://restapi.amap.com/v3/geocode/geo"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        country = data_json['geocodes'][0]['country']
        province = data_json['geocodes'][0]['province']
        city = data_json['geocodes'][0]['city']
        citycode = data_json['geocodes'][0]['citycode']
        district = data_json['geocodes'][0]['district']
        street = data_json['geocodes'][0]['street']
        number = data_json['geocodes'][0]['number']
        adcode = data_json['geocodes'][0]['adcode']
        location = data_json['geocodes'][0]['location']
        level = data_json['geocodes'][0]['level']
        pass

    # 坐标geo转地址
    def get_regeo(self, location):
        postData = {
            "key": self.key,
            "location": location,  # 地址
            "output": "json",  # 返回json
            "radius": "1000",  # 查询POI的半径范围。取值范围：0~3000,单位：米
            "extensions": "all",  #
            "batch": "false",  # batch=true为批量查询。batch=false为单点查询
        }
        url = "https://restapi.amap.com/v3/geocode/regeo"
        html = HttpUtils.do_request("GET", url, "", postData)
        data_json = json.loads(html.text)
        addressComponent = data_json['regeocode']['addressComponent']  # 地址元素列表
        roads_list = data_json['regeocode']['roads']  # 道路信息列表 请求参数 extensions 为 all 时返回如下内容
        roadinters_list = data_json['regeocode']['roadinters']  # 道路交叉口列表 请求参数 extensions 为 all 时返回如下内容
        poi_list = data_json['regeocode']['poi']  # poi信息列表 请求参数 extensions 为 all 时返回如下内容
        aoi_list = data_json['regeocode']['aoi']  # aoi信息列表 请求参数 extensions 为 all 时返回如下内容
        pass


if __name__ == '__main__':
    zhoubian = ZhouBian()
    # 关键字
    for n in range(1, 101):
        zhoubian.get_search(n)
    # 周边
    # for n in range(1, 101):
    #     zhoubian.get_around(n)
    # 多边形
    # for n in range(1, 101):
    #     zhoubian.get_polygon(n)

    # zhoubian.get_search_id("B0014014AD")
    # zhoubian.get_aoi("B0014014AD")
    # 地址转geo地理位置
    # zhoubian.get_geo('北京市朝阳区阜通东大街6号')
    # zhoubian.get_geo('鞍山市民主街67栋')
    zhoubian.get_geo('广东省深圳市福田区石厦新天CBC')
    # 坐标geo转地址
    zhoubian.get_regeo('122.962685,41.120669')